import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/**
 *
 * @author Ryan Brown
 *
 */

public class RegisterUserDialog extends JDialog implements ActionListener{

	// GUI Components
	private JTextField usernameField;
	private JPasswordField  passwordField, verifyPasswordField;
	private JButton createUserButton, cancelButton;
	private JCheckBox[] checkBoxes;
	private String[] positionList =
    {"Supervisor", "Traffic Manager", "Flight Data/Clearance Delivery",
        "Ground Control", "Local Control", "Helicopter Control"
    };
	private final int NUM_POSITIONS = 6;

	/**
	 * Constructor, creates the gui components and displays them
	 * @param parent  JFrame from which this dialog is spawned
	 */
	public RegisterUserDialog(JFrame parent) {
		super(parent, "Register New User", true);	// make the dialog modal

		Container contentPane = getContentPane();
		// Create and set a layout manager to SpringLayout
		contentPane.setLayout(new GridLayout(7, 2, 10, 10));

		// create the text/password fields
		usernameField = new JTextField(20);
		passwordField = new JPasswordField(20);
		verifyPasswordField = new JPasswordField(20);

		// create the checkboxes
		checkBoxes = new JCheckBox[NUM_POSITIONS];
		for(int i = 0; i < NUM_POSITIONS; i++) {
			checkBoxes[i] = new JCheckBox(positionList[i]);
		}

		// create the buttons
		createUserButton = new JButton("Create User");
		cancelButton = new JButton("Cancel");

		// add action listeners to the buttons
		createUserButton.addActionListener(this);
		cancelButton.addActionListener(this);

		// add components to the dialog content pane
		contentPane.add(new JLabel("User Name:"));
		contentPane.add(usernameField);
		contentPane.add(new JLabel("Password:"));
		contentPane.add(passwordField);
		contentPane.add(new JLabel("Verify Password:"));
		contentPane.add(verifyPasswordField);
		for (JCheckBox jcb : checkBoxes)
			contentPane.add(jcb);
		contentPane.add(createUserButton);
		contentPane.add(cancelButton);

		setSize(400,280);

	}

	public void actionPerformed(ActionEvent e) {
		// which button was clicked?
		if (e.getSource() == createUserButton) { // createUserButton was clicked
			// make sure that all fields are filled in
			String username = usernameField.getText();
			String password = new String(passwordField.getPassword());
			String verifyPassword = new String(verifyPasswordField.getPassword());
			if (!"".equals(username) && !"".equals(password)
					&& !"".equals(verifyPassword)) {
				// check to see that the passwords match each other

				if (password.equals(verifyPassword)) {
					// create the new user
					User newUser = new User(username, password);
					// for each checkbox, add the position that are selected to the new user
					for (JCheckBox jcb : checkBoxes) {
						if (jcb.isSelected()) {
							newUser.addPosition(jcb.getText());
						}
					}
					// save the user
					SessionManager.getInstance().saveUser(newUser);
					// close the create user dialog
					dispose();
				}else {
					JOptionPane.showMessageDialog(this, "Passwords do not match!");
				}
			}else {
				JOptionPane.showMessageDialog(this,	"All fields must be filled in.");
			}

		}else if(e.getSource() == cancelButton) { // cancelButton was clicked
			// close the dialog
			dispose();
		}

	}
}
